// pages/vip/vip.ts
import { getPublicPaddingTop } from '../../utils/page'
import { getVIPConfig, ordersAdd, pay, getPersonalInformation } from '../../utils/controller'
const VIP_TYPE_LIST = ['天', '周', '月', '季', '年']
Page({
  /**
   * 页面的初始数据
   */
  data: {
    paddingTop: getPublicPaddingTop(),
    select: 1,
    vipInfo: [],
    userInfo:{},
    button:'立即开通'
  },
  goBack(){
  //   wx.switchTab({
  //     url:"/pages/home/home"
  //  })
  
    wx.navigateBack()
  },

  selectVIPItem(e) {
    this.setData({
      select: +e.currentTarget.dataset.index,
    })
    this.upShow()
  },
  // 添加会员订单
  async ordersAdd(){
    let data = {grade_id:  this.data.vipInfo[this.data.select]['id']}
    let orders_sn = await ordersAdd(data)
    if(orders_sn){
      this.pay(orders_sn)
    }
    
  },
  async pay(orders_sn){
    let moneyData:any = await pay({orders_sn})
    // nonceStr: "IIqdVZQYTVJoqMlDtjkwuMiiTgtBbtqe"
    // out_trade_no: "202405291659588641"
    // package: "prepay_id=wx29165958499597ff311caa8963f2740001"
    // paySign: "8C3A3ABCA16AA94FA291B20B83156E94"
    // signType: "MD5"
    // state: 200
    // timeStamp: "1716973198"
    const t = this
    wx.requestPayment({
      timeStamp: moneyData.timeStamp,
      nonceStr: moneyData.nonceStr,
      package: moneyData.package,
      signType: moneyData.signType,
      paySign: moneyData.paySign,
      success(res) {
        setTimeout(() => {
          t.getUserInfo()
        },300)
        
      },
      fail(res) {
        console.log({res});
        
        wx.showToast({
          title: "支付失败！",
          icon: "error"
        })
      }
    })
    console.log(moneyData)
    
  },
  async getUserInfo(){
    const personalInfoResult:any = await getPersonalInformation()
    if(personalInfoResult){
      this.setData({userInfo:personalInfoResult})
      wx.setStorageSync('userInfo', JSON.stringify(personalInfoResult))
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const _this = this
    let userInfo = wx.getStorageSync('userInfo')
    console.log('userInfo',userInfo);
    if(userInfo){
      this.setData({userInfo:JSON.parse(userInfo)})
      console.log('userInfo', JSON.parse(userInfo));
      
    }
    getVIPConfig()
      .then((result: any) => {
        console.log('获取会员信息', result)
        const vipInfo = result.map((item: any) => {
          item.price = parseInt(item.price)
          item.yuan_price = parseInt(item.yuan_price)
          item.typeText = VIP_TYPE_LIST[item.type]

          return item
        })
        _this.setData({ vipInfo })
      })
      .catch(() => {
        wx.showToast({ title: '获取会员信息错误', icon: 'error' })
      })
  },
  // 判断当前是不是升级
  upShow(){
    let userInfo = this.data.userInfo
    
    if(userInfo.is_member === 1){
      
      
      // button
      if(userInfo.grade_id < (this.data.select + 1)){
        this.setData({button:'立即升级'})
      }else{
        this.setData({button:'立即开通'})
      }
    }else{
      this.setData({button:'立即开通'})
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.upShow()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
})
